package com.morpheus.redis.redisson.single;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class OnEnableRedissonSingleCondition extends SpringBootCondition {
	private static final Logger LOGGER = LoggerFactory.getLogger(OnEnableRedissonSingleCondition.class);

	public OnEnableRedissonSingleCondition() {
	}

	@Override
	public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
		Environment environment = context.getEnvironment();

		boolean isSingle = (environment.containsProperty("spring.redis.host")
						&& environment.containsProperty("spring.redis.port"));
		boolean isRedisson = environment.containsProperty("spring.redis.redisson.config");
		boolean match = isSingle && isRedisson;
		LOGGER.debug("OnEnableRedissonSingleCondition.getMatchOutcome() isSingle={}, isRedisson={}, match={}", isSingle,
				isRedisson, match);
		ConditionOutcome conditionOutcome = new ConditionOutcome(match, "Enabled Redis Single client Redisson.");
		return conditionOutcome;
	}
}
